﻿'
' Twitter Authentication Provider - http://www.oliverhine.com
' Copyright (c) 2009
' by Oliver Hine
'

Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Globalization

Imports DotNetNuke.Entities.Modules

Namespace OliverHine.Authentication.Twitter

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The AuthenticationConfig class providesa configuration class for the Twitter
    ''' Authentication provider
    ''' </summary>
    ''' <history>
    ''' 	[ohine]	07/14/2009  Created Twitter Authentication Provider
    ''' </history>
    ''' -----------------------------------------------------------------------------
    <Serializable()> _
    Public Class AuthenticationConfig
        Inherits DotNetNuke.Services.Authentication.AuthenticationConfigBase

#Region "Private Members"

        Private _Enabled As Boolean = False
        Private _AutoFollow As Boolean = Null.NullBoolean
        Private _AppUserName As String = String.Empty
        Private _key As String = String.Empty
        Private _secret As String = String.Empty
        Private _AutoTweet As Boolean = Null.NullBoolean
        Private _tweetThisMessage As String = String.Empty

        Private Const CACHEKEY As String = "Auth.Twitter"

#End Region

#Region "Constructor(s)"

        Protected Sub New(ByVal portalID As Integer)
            MyBase.New(portalID)

            Try
                'Dim setting As String = Null.NullString
                'If Entities.Portals.PortalController.GetPortalSettingsDictionary(portalID).TryGetValue("Tweetification_Enabled", setting) Then
                ' _Enabled = Boolean.Parse(setting)
                'End If
                'setting = Null.NullString
                'If Entities.Portals.PortalController.GetPortalSettingsDictionary(portalID).TryGetValue("Tweetification_ConsumerKey", setting) Then
                '_key = setting
                'End If
                'If Entities.Portals.PortalController.GetPortalSettingsDictionary(portalID).TryGetValue("Tweetification_ConsumerSecret", setting) Then
                '_secret = setting
                'End If
                'If Entities.Portals.PortalController.GetPortalSettingsDictionary(portalID).TryGetValue("Tweetification_AppUserName", setting) Then
                '_AppUserName = setting
                'End If
                'If Entities.Portals.PortalController.GetPortalSettingsDictionary(portalID).TryGetValue("Tweetification_AutoFollow", setting) Then
                ' _AutoFollow = Boolean.Parse(setting)
                'End If
                Dim hsh As Hashtable = Entities.Portals.PortalSettings.GetSiteSettings(portalID)

                If hsh.ContainsKey("Tweetification_Enabled") Then _Enabled = Boolean.Parse(Entities.Portals.PortalSettings.GetSiteSetting(portalID, "Tweetification_Enabled"))
                If hsh.ContainsKey("Tweetification_ConsumerKey") Then _key = Entities.Portals.PortalSettings.GetSiteSetting(portalID, "Tweetification_ConsumerKey")
                If hsh.ContainsKey("Tweetification_ConsumerSecret") Then _secret = Entities.Portals.PortalSettings.GetSiteSetting(portalID, "Tweetification_ConsumerSecret")
                If hsh.ContainsKey("Tweetification_AppUserName") Then _AppUserName = Entities.Portals.PortalSettings.GetSiteSetting(portalID, "Tweetification_AppUserName")
                If hsh.ContainsKey("Tweetification_AutoFollow") Then _AutoFollow = Boolean.Parse(Entities.Portals.PortalSettings.GetSiteSetting(portalID, "Tweetification_AutoFollow"))
                If hsh.ContainsKey("Tweetification_AutoTweet") Then _AutoTweet = Boolean.Parse(Entities.Portals.PortalSettings.GetSiteSetting(portalID, "Tweetification_AutoTweet"))
                If hsh.ContainsKey("Tweetification_TweetThis") Then _tweetThisMessage = Entities.Portals.PortalSettings.GetSiteSetting(portalID, "Tweetification_TweetThis")

            Catch
            End Try
        End Sub

#End Region

#Region "Public Properties"

        Public Property Enabled() As Boolean
            Get
                Return _Enabled
            End Get
            Set(ByVal value As Boolean)
                _Enabled = value
            End Set
        End Property
        Public Property AutoTweet() As Boolean
            Get
                Return _AutoTweet
            End Get
            Set(ByVal value As Boolean)
                _AutoTweet = value
            End Set
        End Property
        Public Property TweetThis() As String
            Get
                Return _tweetThisMessage
            End Get
            Set(ByVal value As String)
                _tweetThisMessage = value
            End Set
        End Property
        Public Property AutoFollow() As Boolean
            Get
                Return _AutoFollow
            End Get
            Set(ByVal value As Boolean)
                _AutoFollow = value
            End Set
        End Property
        Public Property AppUserName() As String
            Get
                Return _AppUserName
            End Get
            Set(ByVal value As String)
                _AppUserName = value
            End Set
        End Property
        Public Property ConsumerKey() As String
            Get
                Return _key
            End Get
            Set(ByVal value As String)
                _key = value
            End Set
        End Property

        Public Property ConsumerSecret() As String
            Get
                Return _secret
            End Get
            Set(ByVal value As String)
                _secret = value
            End Set
        End Property


#End Region

#Region "Public SHared Methods"

        Public Shared Sub ClearConfig(ByVal portalId As Integer)
            Dim key As String = CACHEKEY + "_" + portalId.ToString()
            DataCache.RemoveCache(key)
        End Sub

        Public Shared Function GetConfig(ByVal portalId As Integer) As AuthenticationConfig

            Dim key As String = CACHEKEY + "_" + portalId.ToString()
            Dim config As AuthenticationConfig = CType(DataCache.GetCache(key), AuthenticationConfig)

            If config Is Nothing Then
                config = New AuthenticationConfig(portalId)
                DataCache.SetCache(key, config)
            End If
            Return config
        End Function

        Public Shared Sub UpdateConfig(ByVal config As AuthenticationConfig)
            ' Dim objPortalController As Entities.Portals.PortalController
            Entities.Portals.PortalSettings.UpdateSiteSetting(config.PortalID, "Tweetification_Enabled", config.Enabled.ToString())
            Entities.Portals.PortalSettings.UpdateSiteSetting(config.PortalID, "Tweetification_ConsumerKey", config.ConsumerKey.ToString())
            Entities.Portals.PortalSettings.UpdateSiteSetting(config.PortalID, "Tweetification_ConsumerSecret", config.ConsumerSecret.ToString())
            Entities.Portals.PortalSettings.UpdateSiteSetting(config.PortalID, "Tweetification_AppUserName", config.AppUserName.ToString())
            Entities.Portals.PortalSettings.UpdateSiteSetting(config.PortalID, "Tweetification_AutoFollow", config.AutoFollow.ToString())
            Entities.Portals.PortalSettings.UpdateSiteSetting(config.PortalID, "Tweetification_AutoTweet", config.AutoTweet.ToString())
            Entities.Portals.PortalSettings.UpdateSiteSetting(config.PortalID, "Tweetification_TweetThis", config.TweetThis.ToString())

            ClearConfig(config.PortalID)
        End Sub

#End Region

    End Class

End Namespace
